package imli.me.lquan.base.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.MenuRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;

import butterknife.ButterKnife;
import imli.me.lquan.ImApp;
import imli.me.lquan.utils.ActivityUtils;

/**
 * Created by Em on 2017/12/19.
 */

public abstract class BaseActivity extends AppCompatActivity {

    // TAG
    private static String TAG = null;
    private ImApp mApp = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityUtils.getActivityLifecycleCallbacks().onActivityCreated(this, savedInstanceState);
        this.initialization();
        this.setContentView(inflateLayout());
        ButterKnife.bind(this);
    }

    /**
     * initialization
     */
    protected void initialization() {
        this.TAG = getClass().getSimpleName();
        this.mApp = (ImApp) getApplication();
    }

    /**
     * get contentView layoutRes
     * @return contentView layoutRes
     */
    protected abstract @LayoutRes int inflateLayout();

    /**
     * get menu res
     * @return menu
     */
    protected @MenuRes int inflateMenu() {
        return -1;
    }

    /**
     * get application
     * @return
     */
    public ImApp getApp() {
        return mApp;
    }

    /**
     * get activity context
     * @return
     */
    public Context getContext() {
        return this;
    }

    /**
     * get activity tag
     * @return
     */
    public static String getTag() {
        if (null != TAG) {
            return TAG;
        }
        return "Activity";
    }

    @Override
    protected void onStart() {
        ActivityUtils.getActivityLifecycleCallbacks().onActivityStarted(this);
        super.onStart();
    }

    @Override
    protected void onStop() {
        ActivityUtils.getActivityLifecycleCallbacks().onActivityStopped(this);
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        ActivityUtils.getActivityLifecycleCallbacks().onActivityDestroyed(this);
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        ActivityUtils.getActivityLifecycleCallbacks().onActivityPaused(this);
        super.onPause();
    }

    @Override
    protected void onResume() {
        ActivityUtils.getActivityLifecycleCallbacks().onActivityResumed(this);
        super.onResume();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        ActivityUtils.getActivityLifecycleCallbacks().onActivitySaveInstanceState(this, outState);
        super.onSaveInstanceState(outState);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (inflateMenu() != -1) {
            getMenuInflater().inflate(inflateMenu(), menu);
        }
        return super.onCreateOptionsMenu(menu);
    }

}
